package com.flashcash.solu.view.loan;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.flashcash.kotlin.data.ProductBean;
import com.flashcash.kotlin.fragment.LoanNormalFragment1;
import com.flashcash.solu.R;
import com.flashcash.solu.app.base.BaseActivity;
import com.flashcash.solu.common.EventCollection;
import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;

public class LoanNormalFragment1Activity extends BaseActivity<LoanNormalActPresenter> {

    private static final String KEY_PRODUCT_BEAN = "product_bean";
    private static final String KEY_BUNDLE = "bundle";

    private ProductBean productBean;
    private boolean gotoLoanInfo;

    public static void startActivity(Context context, ProductBean productBean) {
        Intent intent = new Intent(context, LoanNormalFragment1Activity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_PRODUCT_BEAN, productBean);
        intent.putExtra(KEY_BUNDLE, bundle);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_loan_normal_fragment1;
    }

    @Override
    protected void init() {
        //初始化productBean
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getBundleExtra(KEY_BUNDLE);
            if (bundle != null) {
                productBean = (ProductBean) bundle.getSerializable(KEY_PRODUCT_BEAN);
            }
        }

        RxBus.get().register(this);

        if (productBean != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.container, LoanNormalFragment1.Companion.newInstance(productBean)).commitAllowingStateLoss();
        }
    }

    /**
     * event posted from: called SubmittedLoanActivity
     *
     * @param event rx bus event
     */
    @Subscribe
    public void onGotoMyLoan(EventCollection.GotoMyLoan event) {
        gotoLoanInfo = true;
        finish();
    }

    /**
     * event posted from: called FaceDetectorFragment
     *
     * @param event rx bus event
     */
    @Subscribe
    public void onFinish(EventCollection.FinishLoanNormalFragment1Activity event) {
        gotoLoanInfo = true;
        finish();
    }

    @Override
    protected LoanNormalActPresenter initPresenterImpl() {
        return new LoanNormalPreImp();
    }

    @Override
    protected void onDestroy() {
        if (!gotoLoanInfo) {
            RxBus.get().post(new EventCollection.LoanEvent());
        }
        RxBus.get().unregister(this);
        super.onDestroy();
    }
}
